/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet.Test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author boomar
 */
public class Vote extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Votez MARC</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<marquee onmouseout="this.start();" onmouseover="this.stop();" scrollamount="5" direction="left" align="middle"><h1> Votez MARC ! </h1></marquee>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String nom = request.getParameter("nom");
        String prenom = request.getParameter("prenom");

        if (!nom.isEmpty() && !prenom.isEmpty()) {
            out.println("<html><title>Votez " + nom + " " + prenom + "! </title>");
            out.println("<body>");
            out.println("<h1>Votez " + nom + " " + prenom + "</h1>");
            out.println("</body></html>");
        } else {
            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Election 2010/2011 </title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Vote Candidat </h1>");
            out.println("<p color=\"red\">" + (nom.isEmpty() && !prenom.isEmpty() ? nom : prenom) + "</p>");
            out.println("<form method=\"POST\">");
            out.println("Nom : <input type=\"text\" name=\"nom\" value=\"" + nom + "\">");
            out.println("Prenom : <input type=\"text\" name=\"prenom\" value=\"" + prenom + "\">");
            out.println("<input type=\"submit\" value=\"valider\">");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
        }
        out.close();
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String nom = request.getParameter("nom");
        String prenom = request.getParameter("prenom");
        out.println("<html><title>Votez " + nom + " " + prenom + "! </title>");
        out.println("<body>");
        out.println("<h1>Votez " + nom + " " + prenom + "</h1>");
        out.println("</body></html>");
    }
    
}
